package com.simon.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.simon.http.model.PResponse;
import com.simon.system.dao.DeptMapper;
import com.simon.system.model.Dept;
import com.simon.system.service.DeptService;
import com.simon.utils.Utils;

@Service
public class DeptServiceImpl implements DeptService {
	
	@Autowired
	private DeptMapper deptMapper;
	
	
	@Override
	public int addDept(Dept dept) {
		return deptMapper.insert(dept);
	}

	@Override
	public Dept getDeptById(int id) {
		return deptMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Dept> queryDepts(String name, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Dept> depts = deptMapper.queryDepts(name);
		return depts;
	}

	@Override
	public PResponse calPage(String name, int pageSize) {
		List<Dept> depts = deptMapper.queryDepts(name);
		PResponse bPage = new PResponse();
		if (!depts.isEmpty()) {
			bPage.setTotalElements(depts.size());
			bPage.setTotalPage(Utils.calPages(depts.size(),pageSize));
		}else {
			bPage.setTotalElements(0);
			bPage.setTotalPage(0);
		}
		
		return bPage;
	}

	@Override
	public int deleteDept(int id) {
		return deptMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updateDept(Dept oldDept, Dept newDept) {
		
		oldDept.setFullname(newDept.getFullname());
		oldDept.setPid(newDept.getPid());
		oldDept.setPids(newDept.getPids());
		oldDept.setSimplename(newDept.getSimplename());
		oldDept.setTips(newDept.getTips());
		
		return deptMapper.updateByPrimaryKey(oldDept);
	}

	@Override
	public List<Dept> sublist(int pid) {
		return deptMapper.selectByPid(pid);
	}

	@Override
	public List<Dept> findAllDepts() {		
		return deptMapper.selectDepts();
	}

	@Override
	public Dept findByName(String name) {
		return deptMapper.findByName(name);
	}
	
	

}
